#include "resetpwd.h"
#include "ui_resetpwd.h"
#include <QMessageBox>
#include <QSqlQuery>

Resetpwd::Resetpwd(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Resetpwd)
{
    ui->setupUi(this);
}

Resetpwd::~Resetpwd()
{
    delete ui;
}

void Resetpwd::setAccountAndQuestion(const QString& account, const QString& question, const QString& answer)
{
    m_account = account;
    m_answer = answer;
    ui->secretQuestionLabel->setText(question);
}

void Resetpwd::on_resetPwdSubmitBtn_clicked()
{
    QString answer = ui->secretAnswerEdit->text().trimmed();
    QString newPwd = ui->newPasswordEdit->text();
    QString confirmPwd = ui->confirmPasswordEdit->text();

    if (answer != m_answer) {
        QMessageBox::warning(this, "提示", "密保问题答案错误！");
        return;
    }
    if (newPwd.isEmpty() || confirmPwd.isEmpty()) {
        QMessageBox::warning(this, "提示", "请输入新密码！");
        return;
    }
    if (newPwd != confirmPwd) {
        QMessageBox::warning(this, "提示", "两次输入的密码不一致！");
        return;
    }
    QSqlQuery query;
    query.prepare("UPDATE users SET password=? WHERE id=?");
    query.addBindValue(newPwd);
    query.addBindValue(m_account);
    if (query.exec()) {
        QMessageBox::information(this, "提示", "密码修改成功！");
        this->close();
    } else {
        QMessageBox::critical(this, "错误", "密码修改失败！");
    }
}
